package com.demo.rxjavademo;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.demo.rxjavademo.http.Http;
import com.demo.rxjavademo.loader.FrescoImageLoader;
import com.demo.rxjavademo.mode.SplashInfo;
import com.facebook.drawee.view.SimpleDraweeView;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;


/**
 * 你可以在 Publisher 中查询数据库或者从网络上获取数据，然后在 Subscriber 中显示。
 * Publisher 不只有一种，事实上 Flowable 和 Processor 所有的子类都属于 Publisher。
 * 在数据发射途中，你可以利用操作符对数据进行变换。
 */
public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
    private TextView mTextView;
    private Button mButton;
    private SimpleDraweeView mSplashView;
    private Context mContext = null;
    private FrescoImageLoader frescoImageLoader;

    /**
     * 订阅者做的事情尽量越少越好，订阅者大多在主线程运行
     */
    Subscriber mSubscriber = new Subscriber<SplashInfo>() {
        @Override
        public void onSubscribe(Subscription s) {
            Log.e(TAG, "onSubscribe");
            s.request(Long.MAX_VALUE);
            //通常在这儿请求资源，参数就是要请求的数量

        }

        @Override
        public void onNext(SplashInfo splashInfo) {

            Log.e(TAG, "onNext");
            Log.d(TAG, splashInfo.getImg());
            Log.d(TAG, splashInfo.getText());
            mTextView.setText(splashInfo.getText());
            frescoImageLoader.displayImage(mContext, splashInfo.getImg(), mSplashView);

        }


        @Override
        public void onError(Throwable t) {
            Log.e(TAG, "onError");

        }

        @Override
        public void onComplete() {
            Log.e(TAG, "onComplete");

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//
        mTextView = (TextView) findViewById(R.id.text);
        mSplashView = (SimpleDraweeView) findViewById(R.id.iv_splash);
        mButton = (Button) findViewById(R.id.button);
        mButton.setVisibility(View.INVISIBLE);
        frescoImageLoader = new FrescoImageLoader();
        mContext = this;
        getSplash();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    public void getSplash() {
        Http.getInstance().getSplash(mSubscriber, "1080*720");
    }
}
